package com.woldier.zuul.filter;

import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;

import com.woldier.zuul.common.UrlPatentProperties;
import com.woldier.zuul.entity.File;
import com.woldier.zuul.service.FileUpdateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.POST_TYPE;

/**
 * 后置处理
 * 处理是update请求的
 */
@Slf4j
@Component
@EnableConfigurationProperties({UrlPatentProperties.class, ZuulProperties.class})
public class PostFilter extends BaseFilter{
    @Autowired
    ZuulProperties zuulProperties;
    @Autowired
    UrlPatentProperties urlPatentProperties;
    @Autowired
    FileUpdateService fileUpdateService;

    @Value("${woldier.hadoop.prefix}")
    private String prefix;


    /**
     * 保存url
     */
    private String uri;
    @Override
    public String filterType() {
        return POST_TYPE;
    }

    @Override
    public int filterOrder() {
        return 900;
    }

    /**
     * 通过购置处理 判断哪些路由需要被后置处理
     * @return
     */
    @Override
    public boolean shouldFilter() {
        RequestContext ctx = RequestContext.getCurrentContext();
        HttpServletRequest request = ctx.getRequest();
        String s = request.getRequestURI();
        String[] ops = request.getParameterValues("op");
        log.info(s+"===============>post process");
        if(s.contains("/api/hadoop/")) {
            log.info("请求了hadoop服务");
            for (String op : ops) {
                if("CREATE".equals(op)) {
                    int code = RequestContext.getCurrentContext().getResponseStatusCode();
                    /*如果是上传类型且状态吗为201则进行filter操作*/
                    if(code == HttpStatus.OK.value() || code == HttpStatus.CREATED.value()){
                        this.uri = s;
                        return true;
                    }
                }
            }
        }
        return false;


    }



    /**
     * 文件上传成功保存文件信息到数据库
     * @return
     * @throws ZuulException
     */
    @Override
    public Object run() throws ZuulException {

        /**
         * 更具uri路径中的用户名+repo名设置对应字段
         * /api/hadoop/data/woldier/remote-repo/dir/file-temp.txt?op=dasdada
         */
        /*对url进行解析 */
        /*过滤掉前缀*/
        /*对url进行解析 */
        /*过滤掉前缀*/
        String substring = uri.substring(prefix.length());
        int index = substring.indexOf('/');
        index = substring.indexOf('/',index+1);
        /*得到用户名与仓库名的拼接*/
        /* /woldier/remote-repo */
        String name_repo = substring.substring(0,index);

        //TODO 加入update时间
        //int index_last = substring.indexOf('?');
        /*得到path+filename 的拼接*/
        /* /dir/file-temp.txt */
        String file_path = substring.substring(index);

        /*存入数据库*/
        boolean b = fileUpdateService.update(File.builder().pathFile(file_path).nameRepo(name_repo).build());
        return null;
    }
}
